< previous page page_161 next page >

Page 161
The remainder of the name can contain letters, numbers, and/or underscore characters. No spaces, periods, or other punctuation characters are allowed.
The name must be unique within the variable's scope. (Scope refers to the context in which the variable is defined, as we will see shortly.)
The name can be no longer than 255 characters.
The name cannot be one of Visual Basic's reserved words.
Make your variable names descriptive of the task to make your code easy to read, but also keep the names as short as possible to make the code easy to type. Many programmers also use prefixes in their variable names to indicate the type of data stored; usually, these conventions are variations of the Hungarian Naming Convention. These prefixes usually consist of one or two lowercase characters at the beginning of the variable; the next letter is usually capitalized.
For example, a prefix of s, as in sCity, indicates a variable that stores a String value. Table 8.1 lists some of the common prefixes that you should use when naming variables. This convention, which is recommended in a Microsoft Knowledge Base article, is used for many of the original code samples in this book. You might prefer a two-character prefix or some other convention altogether.
Table 8.1 Variable Naming Prefixes
Variable TypePrefixExample
StringssFirstName
IntegernnAge
Long IntegerllPopulation
Single (Floating Point)ffAverageGrade
DoubleddThrustRatio
CurrencyccPayRate
BooleanbbTaxable

For integers, the prefix n is used rather than the seemingly more obvious i because the prefix i is used to represent indexes.
Types of Variables
Okay, you know what a variable does and how to name it. But what can you store in a variable? The simple answer is: almost anything. A variable can hold a number; a string of text; or a reference to an object, such as a form, control, or database. This chapter looks specifically at using variables to store numbers, strings, and logical values. Using variables to represent objects and databases is covered later in Chapters 16, ''Classes: Reusable Components," and 26, "Using Data Access Objects (DAO)," respectively.

 
< previous page page_161 next page >